package champPackage;

import java.util.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ScoreUpdateServlet
 */
@WebServlet("/ScoreUpdateServlet")
public class ScoreUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ScoreUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String firstTeam = (String)request.getParameter("firstTeam");
		String secondTeam = (String)request.getParameter("secondTeam");
		String result = (String)request.getParameter("matchResult");
		int firstId = Integer.parseInt((String) (request.getParameter("firstTeamID")));
		int secondId = Integer.parseInt((String) (request.getParameter("secondTeamID")));
		int index = result.indexOf(':');
		int firstScored = Integer.parseInt(result.substring(0, index));
		int secondScored = Integer.parseInt(result.substring(index+1));
		int firstPoint = (firstScored>secondScored)? 3: (firstScored==secondScored? 1: 0);
		int secondPoint = firstPoint==3? 0: (firstPoint==1? 1: 3);
		int champId = (Integer)((request.getServletContext().getAttribute("champID")));
		DBManager db = DBManager.getInstance();
		TableLine firstLine = db.getTableLine(champId, firstId);
		TableLine secondLine = db.getTableLine(champId, secondId);
		System.out.println(firstLine==null);
		firstLine.setPlayed(firstLine.getPlayed()+1);
		secondLine.setPlayed(secondLine.getPlayed()+1);
		if(firstScored>secondScored){
			firstLine.setWon(firstLine.getWon()+1);
			secondLine.setLost(secondLine.getLost()+1);
		}
		else if(firstScored<secondScored){
			firstLine.setLost(firstLine.getLost()+1);
			secondLine.setWon(secondLine.getWon()+1);
		}
		else{
			firstLine.setDraws(firstLine.getDraws()+1);
			secondLine.setDraws(secondLine.getDraws()+1);
		}
		firstLine.setScored(firstLine.getScored()+firstScored);
		secondLine.setScored(secondLine.getScored()+secondScored);
		firstLine.setReceived(firstLine.getReceived()+secondScored);
		secondLine.setReceived(secondLine.getReceived()+firstScored);
		System.out.println(firstPoint+" "+secondPoint);
		firstLine.setPoint(firstLine.getPoint()+firstPoint);
		secondLine.setPoint(secondLine.getPoint()+secondPoint);
		System.out.println(firstLine.getPoint()+" point");
		TournamentTable tournament = new TournamentTable(champId);
		db.updateChampionshipTableLine(firstLine);
		db.updateChampionshipTableLine(secondLine);
		for(int i=1; i<=4; i++){
			tournament.addNewLineToTable(db.getTableLine(champId, i));
		}
		Iterator<TableLine> lines = tournament.getSortedTableLines();
		while(lines.hasNext()){
			System.out.println(lines.next().toString());
		}
		System.out.println("shemovida");
	}

}
